﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class NhapGiaDinhMuc : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmNhapGiaDinhMuc";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    
    public string XM = System.Configuration.ConfigurationSettings.AppSettings["IDXiMang"].ToString();
    public string Nuoc = System.Configuration.ConfigurationSettings.AppSettings["IDNuoc"].ToString();
    public string Cat = System.Configuration.ConfigurationSettings.AppSettings["IDCat"].ToString();
    public string Da12 = System.Configuration.ConfigurationSettings.AppSettings["IDDa12"].ToString();
    public string Da24 = System.Configuration.ConfigurationSettings.AppSettings["IDDa24"].ToString();
    static int PageNumber = 15;
    static int CurrentPage = 1;
    static System.Data.DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {


                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;
                    Title = "Quản lý giá định mức vật liệu | " + Header;
                    for (int i = 1; i <= 12; i++)
                    {
                        dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                        dlThangLoc.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                    }
                    dlThang.Items.Insert(0, new ListItem("--Chọn tháng--", ""));
                    dlThang.SelectedIndex = 0;

                    
                    dlThangLoc.SelectedValue = DateTime.Now.Month.ToString();

                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                        dlNamLoc.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    dlNam.SelectedValue = year2.ToString();
                    dlNamLoc.SelectedValue = year2.ToString();
                    btnCancel_Click(sender, e);
                    LoadMac(dlMac);
                    LoadMac(dlMacLoc);
                    LoadPhuGia(dlPhuGia);
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (hdIDDinhMuc.Value.Trim().Equals(""))
            gstGetMess("Chưa có dữ liệu định mức vật liệu", "");
        else
        {
            Guid gID = new Guid(hdIDDinhMuc.Value);
            p = new SqlParameter[14];
            p[0] = new SqlParameter("@IDs", Guid.NewGuid());
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            p[1] = new SqlParameter("@IDDinhMuc", gID);
            p[1].SqlDbType = SqlDbType.UniqueIdentifier;
            p[2] = new SqlParameter("@DGXM", double.Parse(txtDGXM.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGXM.Text.Trim().Replace(",", "")));
            p[2].SqlDbType = SqlDbType.Float;
            p[3] = new SqlParameter("@DGCat", double.Parse(txtDGCat.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGCat.Text.Trim().Replace(",", "")));
            p[3].SqlDbType = SqlDbType.Float;
            p[4] = new SqlParameter("@DGDa", double.Parse(txtDGDa.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGDa.Text.Trim().Replace(",", "")));
            p[4].SqlDbType = SqlDbType.Float;
            p[5] = new SqlParameter("@DGNuoc", double.Parse(txtDGNuoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGNuoc.Text.Trim().Replace(",", "")));
            p[5].SqlDbType = SqlDbType.Float;
            p[6] = new SqlParameter("@DGPhuGia", double.Parse(txtDGPhuGia.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGPhuGia.Text.Trim().Replace(",", "")));
            p[6].SqlDbType = SqlDbType.Float;

            p[7] = new SqlParameter("@DGXMVAT", double.Parse(txtDGXMVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGXMVAT.Text.Trim().Replace(",", "")));
            p[7].SqlDbType = SqlDbType.Float;
            p[8] = new SqlParameter("@DGCatVAT", double.Parse(txtDGCatVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGCatVAT.Text.Trim().Replace(",", "")));
            p[8].SqlDbType = SqlDbType.Float;
            p[9] = new SqlParameter("@DGDaVAT", double.Parse(txtDGDaVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGDaVAT.Text.Trim().Replace(",", "")));
            p[9].SqlDbType = SqlDbType.Float;
            p[10] = new SqlParameter("@DGNuocVAT", double.Parse(txtDGNuocVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGNuocVAT.Text.Trim().Replace(",", "")));
            p[10].SqlDbType = SqlDbType.Float;
            p[11] = new SqlParameter("@DGPhuGiaVAT", double.Parse(txtDGPhuGiaVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGPhuGiaVAT.Text.Trim().Replace(",", "")));
            p[11].SqlDbType = SqlDbType.Float;
            p[12] = new SqlParameter("@Thang", int.Parse(dlThang.SelectedValue));
            p[12].SqlDbType = SqlDbType.Int;
            p[13] = new SqlParameter("@Nam", int.Parse(dlNam.SelectedValue));
            p[13].SqlDbType = SqlDbType.Int;

            ex.ExecuteNonQuerry("sp_xdagiadinhmuc_Add", p);
            btnCancel_Click(sender, e);
            gstGetMess("Lưu thành công", "");
        }

    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (hdIDDinhMuc.Value.Trim().Equals(""))
            gstGetMess("Chưa có dữ liệu định mức vật liệu", "");
        else
        {
            Guid gID = new Guid(hdID.Value);
            Guid gIDDM = new Guid(hdIDDinhMuc.Value);
            p = new SqlParameter[14];
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            p[1] = new SqlParameter("@IDDinhMuc", gIDDM);
            p[1].SqlDbType = SqlDbType.UniqueIdentifier;
            p[2] = new SqlParameter("@DGXM", double.Parse(txtDGXM.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGXM.Text.Trim().Replace(",", "")));
            p[2].SqlDbType = SqlDbType.Float;
            p[3] = new SqlParameter("@DGCat", double.Parse(txtDGCat.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGCat.Text.Trim().Replace(",", "")));
            p[3].SqlDbType = SqlDbType.Float;
            p[4] = new SqlParameter("@DGDa", double.Parse(txtDGDa.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGDa.Text.Trim().Replace(",", "")));
            p[4].SqlDbType = SqlDbType.Float;
            p[5] = new SqlParameter("@DGNuoc", double.Parse(txtDGNuoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGNuoc.Text.Trim().Replace(",", "")));
            p[5].SqlDbType = SqlDbType.Float;
            p[6] = new SqlParameter("@DGPhuGia", double.Parse(txtDGPhuGia.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGPhuGia.Text.Trim().Replace(",", "")));
            p[6].SqlDbType = SqlDbType.Float;

            p[7] = new SqlParameter("@DGXMVAT", double.Parse(txtDGXMVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGXMVAT.Text.Trim().Replace(",", "")));
            p[7].SqlDbType = SqlDbType.Float;
            p[8] = new SqlParameter("@DGCatVAT", double.Parse(txtDGCatVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGCatVAT.Text.Trim().Replace(",", "")));
            p[8].SqlDbType = SqlDbType.Float;
            p[9] = new SqlParameter("@DGDaVAT", double.Parse(txtDGDaVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGDaVAT.Text.Trim().Replace(",", "")));
            p[9].SqlDbType = SqlDbType.Float;
            p[10] = new SqlParameter("@DGNuocVAT", double.Parse(txtDGNuocVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGNuocVAT.Text.Trim().Replace(",", "")));
            p[10].SqlDbType = SqlDbType.Float;
            p[11] = new SqlParameter("@DGPhuGiaVAT", double.Parse(txtDGPhuGiaVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDGPhuGiaVAT.Text.Trim().Replace(",", "")));
            p[11].SqlDbType = SqlDbType.Float;
            p[12] = new SqlParameter("@Thang", int.Parse(dlThang.SelectedValue));
            p[12].SqlDbType = SqlDbType.Int;
            p[13] = new SqlParameter("@Nam", int.Parse(dlNam.SelectedValue));
            p[13].SqlDbType = SqlDbType.Int;

            ex.ExecuteNonQuerry("sp_xdagiadinhmuc_Update", p);
            btnCancel_Click(sender, e);
            gstGetMess("Lưu thành công", "");
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        btnSave.Visible = true;
        btncapnhat.Visible = false;

        btnNext.Visible = true;
        btnPre.Visible = true;
        btnFirst.Visible = true;

        dlThang.SelectedIndex = 0;
        dlNam.SelectedIndex = 0;
        dlMac.SelectedIndex = 0;
        dlLoaiDa.SelectedIndex = 0;
        dlPhuGia.SelectedIndex = 0;

        dlMacLoc.SelectedIndex = 0;
        dlLoaiDaLoc.SelectedIndex = 0;

        txtDMCat.Text = "";
        txtDMDa.Text = "";
        txtDMNuoc.Text = "";
        txtDMPhuGia.Text = "";
        txtDMXM.Text = "";

        txtDGCat.Text = "";
        txtDGCatVAT.Text = "";
        txtDGDa.Text = "";
        txtDGDaVAT.Text = "";
        txtDGNuoc.Text = "";
        txtDGNuocVAT.Text = "";
        txtDGPhuGia.Text = "";
        txtDGPhuGiaVAT.Text = "";
        txtDGXM.Text = "";
        txtDGXMVAT.Text = "";

        //txtThanhTienKhongVAT.Text = "";
        //txtThanhTienVAT.Text = "";
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdagiadinhmuc_List",p);
        GV.DataBind();

        
    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_xdagiadinhmuc_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {
                    hdIDDinhMuc.Value = dt.Rows[0]["IDDinhMuc"].ToString();
                    dlThang.SelectedValue = dt.Rows[0]["Thang"].ToString();
                    dlNam.SelectedValue = dt.Rows[0]["Nam"].ToString();
                    dlMac.SelectedValue = dt.Rows[0]["Mac"].ToString();
                    dlLoaiDa.SelectedValue = dt.Rows[0]["LoaiDa"].ToString();
                    dlPhuGia.SelectedValue = dt.Rows[0]["PhuGia"].ToString();

                    txtDMCat.Text = dt.Rows[0]["DMCat"].ToString();
                    txtDMDa.Text = dt.Rows[0]["DMDa"].ToString();
                    txtDMNuoc.Text = dt.Rows[0]["DMNuoc"].ToString();
                    txtDMPhuGia.Text = dt.Rows[0]["DMPhuGia"].ToString();
                    txtDMXM.Text = dt.Rows[0]["DMXM"].ToString();

                    txtDGCat.Text = dt.Rows[0]["DGCat"].ToString();
                    txtDGCatVAT.Text = dt.Rows[0]["DGCatVAT"].ToString();
                    
                    txtDGDa.Text = dt.Rows[0]["DGDa"].ToString();
                    txtDGDaVAT.Text = dt.Rows[0]["DGDaVAT"].ToString();
                    
                    txtDGNuoc.Text = dt.Rows[0]["DGNuoc"].ToString();
                    txtDGNuocVAT.Text = dt.Rows[0]["DGNuocVAT"].ToString();

                    txtDGPhuGia.Text = dt.Rows[0]["DGPhuGia"].ToString();
                    txtDGPhuGiaVAT.Text = dt.Rows[0]["DGPhuGiaVAT"].ToString();

                    txtDGXM.Text = dt.Rows[0]["DGXM"].ToString();
                    txtDGXMVAT.Text = dt.Rows[0]["DGXMVAT"].ToString();
                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tGiá định mức\t" + ax.Message);
                }

            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    try
                    {
                        ex.ExecuteNonQuerry("sp_xdagiadinhmuc_Del", p);
                        btnCancel_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tGiá định mức\t" + ax.Message);
                        gstGetMess(ax.Message, "");
                    }
                }
                else
                    gstGetMess("Bạn không có quyền xóa dữ liệu trên trang này", "");
            }
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdagiadinhmuc_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;

        
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdagiadinhmuc_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdagiadinhmuc_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    void LoadMac(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_MacBeTong_ListDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn mác--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadPhuGia(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaxuathang_ListLoaiPhuGia");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn phụ gia--", ""));
        dl.SelectedIndex = 0;
    }
    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlThang.SelectedIndex > 0 && dlMac.SelectedIndex > 0 && dlLoaiDa.SelectedIndex > 0)
        {
            int index = 1;
            if (dlPhuGia.SelectedIndex > 0)
                index = 1;
            else index = 2;
            p = new SqlParameter[6];
            p[0] = new SqlParameter("@Thang", int.Parse(dlThang.SelectedValue));
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@Nam", int.Parse(dlNam.SelectedValue));
            p[1].SqlDbType = SqlDbType.Int;
            p[2] = new SqlParameter("@Mac", ex.GetGuid(dlMac.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            p[3] = new SqlParameter("@LoaiDa", int.Parse(dlLoaiDa.SelectedValue));
            p[3].SqlDbType = SqlDbType.Int;
            p[4] = new SqlParameter("@PhuGia", ex.GetGuid(dlPhuGia.SelectedValue));
            p[4].SqlDbType = SqlDbType.UniqueIdentifier;
            p[5] = new SqlParameter("@Index", index);
            p[5].SqlDbType = SqlDbType.Int;

            //dt = ex.GetData("sp_xdagiadinhmuc_GetDinhMuc", p);
            dt = ex.GetData("sp_xdagiadinhmuc_LoadGV", p);
            GridView1.DataSource = dt;
            GridView1.DataBind();

            if (dt.Rows.Count > 0)
            {
                //hdIDDinhMuc.Value = dt.Rows[0]["IDs"].ToString();
                //txtDMCat.Text = dt.Rows[0]["DMCat"].ToString();
                //txtDMDa.Text = dt.Rows[0]["DMDa"].ToString();
                //txtDMNuoc.Text = dt.Rows[0]["DMNuoc"].ToString();
                //txtDMPhuGia.Text = dt.Rows[0]["DMPhuGia"].ToString();
                //txtDMXM.Text = dt.Rows[0]["DMXM"].ToString();

                

            }
            else
            {
                hdIDDinhMuc.Value = "";
                txtDMCat.Text = "";
                txtDMDa.Text = "";
                txtDMNuoc.Text = "";
                txtDMPhuGia.Text = "";
                txtDMXM.Text = "";
            }
        }
        else
        {
            hdIDDinhMuc.Value = "";
            txtDMCat.Text = "";
            txtDMDa.Text = "";
            txtDMNuoc.Text = "";
            txtDMPhuGia.Text = "";
            txtDMXM.Text = "";

            

        }
        if (dlThang.SelectedIndex > 0)
        {
            //Xi mang
            string sql = "select top 1 isnull(GiaCoVAT,0) as GiaCoVAT,isnull(GiaKhongVAT,0) as GiaKhongVAT from xdagiabandinhmuc where Thang='" + dlThang.SelectedValue + "' and nam='" + dlNam.SelectedValue + "' and LoaiVL='" + XM + "'";
            dt = ex.GetData_Text(sql);
            if (dt.Rows.Count > 0)
            {
                txtDGXMVAT.Text = dt.Rows[0]["GiaCoVAT"].ToString();
                txtDGXM.Text = dt.Rows[0]["GiaKhongVAT"].ToString();
            }
            else
            {
                txtDGXMVAT.Text = "0";
                txtDGXM.Text = "0";
            }

            //Cat
            sql = "select top 1 isnull(GiaCoVAT,0) as GiaCoVAT,isnull(GiaKhongVAT,0) as GiaKhongVAT from xdagiabandinhmuc where Thang='" + dlThang.SelectedValue + "' and nam='" + dlNam.SelectedValue + "' and LoaiVL='" + Cat + "'";
            dt = ex.GetData_Text(sql);
            if (dt.Rows.Count > 0)
            {
                txtDGCatVAT.Text = dt.Rows[0]["GiaCoVAT"].ToString();
                txtDGCat.Text = dt.Rows[0]["GiaKhongVAT"].ToString();
            }
            else
            {
                txtDGCatVAT.Text = "0";
                txtDGCat.Text = "0";
            }

            //Nuoc
            sql = "select top 1 isnull(GiaCoVAT,0) as GiaCoVAT,isnull(GiaKhongVAT,0) as GiaKhongVAT from xdagiabandinhmuc where Thang='" + dlThang.SelectedValue + "' and nam='" + dlNam.SelectedValue + "' and LoaiVL='" + Nuoc + "'";
            dt = ex.GetData_Text(sql);
            if (dt.Rows.Count > 0)
            {
                txtDGNuocVAT.Text = dt.Rows[0]["GiaCoVAT"].ToString();
                txtDGNuoc.Text = dt.Rows[0]["GiaKhongVAT"].ToString();
            }
            else
            {
                txtDGNuocVAT.Text = "0";
                txtDGNuoc.Text = "0";
            }

            //Loai da

            if (dlLoaiDa.SelectedIndex > 0)
            {
                //da
                if (dlLoaiDa.SelectedValue.Equals("1"))
                    sql = "select top 1 isnull(GiaCoVAT,0) as GiaCoVAT,isnull(GiaKhongVAT,0) as GiaKhongVAT from xdagiabandinhmuc where Thang='" + dlThang.SelectedValue + "' and nam='" + dlNam.SelectedValue + "' and LoaiVL='" + Da12 + "'";
                else
                    sql = "select top 1 isnull(GiaCoVAT,0) as GiaCoVAT,isnull(GiaKhongVAT,0) as GiaKhongVAT from xdagiabandinhmuc where Thang='" + dlThang.SelectedValue + "' and nam='" + dlNam.SelectedValue + "' and LoaiVL='" + Da24 + "'";

                dt = ex.GetData_Text(sql);
                if (dt.Rows.Count > 0)
                {
                    txtDGDaVAT.Text = dt.Rows[0]["GiaCoVAT"].ToString();
                    txtDGDa.Text = dt.Rows[0]["GiaKhongVAT"].ToString();
                }
                else
                {
                    txtDGDaVAT.Text = "0";
                    txtDGDa.Text = "0";
                }
            }
            else
            {
                txtDGDaVAT.Text = "0";
                txtDGDa.Text = "0";
            }

            //PhuGia
            if (dlPhuGia.SelectedIndex > 0)
            {

                sql = "select top 1 isnull(GiaCoVAT,0) as GiaCoVAT,isnull(GiaKhongVAT,0) as GiaKhongVAT from xdagiabandinhmuc where Thang='" + dlThang.SelectedValue + "' and nam='" + dlNam.SelectedValue + "' and LoaiVL='" + dlPhuGia.SelectedValue + "'";

                dt = ex.GetData_Text(sql);
                if (dt.Rows.Count > 0)
                {
                    txtDGPhuGiaVAT.Text = dt.Rows[0]["GiaCoVAT"].ToString();
                    txtDGPhuGia.Text = dt.Rows[0]["GiaKhongVAT"].ToString();
                }
                else
                {
                    txtDGPhuGiaVAT.Text = "0";
                    txtDGPhuGia.Text = "0";
                }
            }
            else
            {
                txtDGPhuGiaVAT.Text = "0";
                txtDGPhuGia.Text = "0";
            }
        }

        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                RadioButton rd = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rdKhachHang");
                if (rd.Checked)
                {
                    Label lb = (Label)GridView1.Rows[i].Cells[11].FindControl("idMaKH");
                    idKhachHang.Value = lb.Text;
                    hdIDDinhMuc.Value = lb.Text;
                }
            }
            //gstGetMess(idKhachHang.Value, "");
            p = new SqlParameter[1];
            Guid gID = new Guid(idKhachHang.Value);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dt = ex.GetData("sp_xdadinhmucvatlieu_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                txtDMCat.Text = dt.Rows[0]["DMCat"].ToString();
                txtDMDa.Text = dt.Rows[0]["DMDa"].ToString();
                txtDMNuoc.Text = dt.Rows[0]["DMNuoc"].ToString();
                txtDMPhuGia.Text = dt.Rows[0]["DMPhuGia"].ToString();
                txtDMXM.Text = dt.Rows[0]["DMXM"].ToString();

            }
            else
            {
                txtDMCat.Text = "";
                txtDMDa.Text = "";
                txtDMNuoc.Text = "";
                txtDMPhuGia.Text = "";
                txtDMXM.Text = "";

            }
        }
        else
        {
            txtDMCat.Text = "";
            txtDMDa.Text = "";
            txtDMNuoc.Text = "";
            txtDMPhuGia.Text = "";
            txtDMXM.Text = "";

        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        int index = 4;
        if (dlMacLoc.SelectedIndex.Equals(0) && dlLoaiDaLoc.SelectedIndex.Equals(0))
            index = 4;
        else if (dlMacLoc.SelectedIndex > 0 && dlLoaiDaLoc.SelectedIndex.Equals(0))
            index = 1;
        else if (dlMacLoc.SelectedIndex > 0 && dlLoaiDaLoc.SelectedIndex > 0)
            index = 3;
        else if (dlMacLoc.SelectedIndex.Equals(0) && dlLoaiDaLoc.SelectedIndex > 0)
            index = 2;

        p = new SqlParameter[5];
        p[0] = new SqlParameter("@Thang", int.Parse(dlThangLoc.SelectedValue));
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@Nam", int.Parse(dlNamLoc.SelectedValue));
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Mac", ex.GetGuid(dlMacLoc.SelectedValue));
        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        p[3] = new SqlParameter("@LoaiDa", int.Parse(dlLoaiDaLoc.SelectedValue));
        p[3].SqlDbType = SqlDbType.Int;
        p[4] = new SqlParameter("@Index", index);
        p[4].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdagiadinhmuc_Search", p);
        GV.DataBind();

        btnNext.Visible = false;
        btnPre.Visible = false;
        btnFirst.Visible = false;
    }
    
}